package com.shengsiyuan.netty.fourthexample.client;

import com.shengsiyuan.netty.thirdexample.client.MyChatClientHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

public class MyClientInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

        ChannelPipeline cp = ch.pipeline();

        cp.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()))
            .addLast(new StringDecoder(CharsetUtil.UTF_8))
            .addLast(new StringEncoder(CharsetUtil.UTF_8))
            .addLast(new MyChatClientHandler());

        //DelimiterBasedFrameDecoder：根据指定的分隔符对接收到的多个ByteBuf进行分割的一个解码器。
    }
}
